package test5.lupeng.jf.com.teamdemo.fragment;


import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.AppCompatButton;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.request.RegisterBean;
import test5.lupeng.jf.com.teamdemo.untils.HTTPUtils;
import test5.lupeng.jf.com.teamdemo.untils.StringUtils;

import static android.content.ContentValues.TAG;

//注册的Fragment
public class RegistFragment extends Fragment {

    @BindView(R.id.ed_registName)
    EditText edRegistName;
    @BindView(R.id.ed_registPassword)
    EditText edRegistPassword;
    @BindView(R.id.et_surePassword)
    EditText etSurePassword;
    @BindView(R.id.bt_regist)
    AppCompatButton btRegist;
    @BindView(R.id.tv_goLogin)
    TextView tvGoLogin;
    Unbinder unbinder;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.regist_fragment, null);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.bt_regist, R.id.tv_goLogin})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_regist:
                String Rname = edRegistName.getText().toString();
                String Rpass = edRegistPassword.getText().toString();
                String Rpass1 = etSurePassword.getText().toString();
                if (StringUtils.checkUserName(Rname, edRegistName) && StringUtils.checkPassword(Rpass, edRegistPassword) && StringUtils.checkPassword(Rpass1, etSurePassword)) {
//                    Toast.makeText(getActivity(), "格式都正确了", Toast.LENGTH_SHORT).show();
                    if (Rpass.equals(Rpass1)) {
                        register(Rpass,Rname);
                    } else {
                        Toast.makeText(getActivity(), "两次密码不同", Toast.LENGTH_SHORT).show();
                    }
                }
                break;
            case R.id.tv_goLogin:
                getFragmentManager().popBackStack();
                break;
        }
    }

    public void register(String password,String username){
        OkHttpClient client=new OkHttpClient();
        RegisterBean bean = new RegisterBean(password, username);
        String url = "https://leancloud.cn:443/1.1/users";
        Request request=HTTPUtils.requestPost(bean, url);
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "注册失败！", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result=response.body().string();
                Log.i(TAG, "onResponse: " + result);
                 getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getActivity(), "注册成功！", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

    }
}
